<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['title', validatorRules.title]" placeholder="请输入标题"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="链接" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['href']" placeholder="请输入链接"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="转到分类" :labelCol="labelCol" :wrapperCol="wrapperCol">
<!--              <a-input v-decorator="['href']" placeholder="请输入链接"></a-input>-->
              <a-cascader  :options="fenleioptions" @change="onChange" placeholder="请选择分类" v-decorator="['goodsCategoryIds', validatorRules.goodsCategoryId]"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="图片" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="图片大小710×300。">
              <j-image-upload :isMultiple="formDisabled" :multiple="false" v-decorator="['bannerUrl']"></j-image-upload>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['type', validatorRules.type]" :trigger-change="true" dictCode="terminal_type" placeholder="请选择类型"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="排序号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['sortNum',validatorRules.sortNum]" placeholder="请输入排序号" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="是否主题" :labelCol="labelCol" :wrapperCol="wrapperCol">
<!--              <j-dict-select-tag type="list" v-decorator="['isTheme']" :trigger-change="true" dictCode="app_theme" placeholder="请选择是否主题"/>-->
                            <j-search-select-tag
                              :list-data.sync="isThemeList"
                              v-decorator="['isTheme', validatorRules.isTheme]"
                              dict="app_theme"
                            />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['isEnabled', validatorRules.isEnabled]" :trigger-change="true" dictCode="valid_status" placeholder="请选择是否启用"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="色值" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['colorValue']" placeholder="请输入色值"></a-input>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JImageUpload from '@/components/jeecg/JImageUpload'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JSearchSelectTag from '@/components/dict/JSearchSelectTag'

  export default {
    name: 'BannerForm',
    components: {
      JFormContainer,
      JImageUpload,
      JDictSelectTag,
      JSearchSelectTag,
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    watch: {
      isThemeList: {
        deep: true,
        immediate: true,
        handler(val) {
          if (val&&!this.model.isTheme) {
            this.$nextTick(() => {
              this.form.setFieldsValue({isTheme:val[0].value})
            })
          }
        },
      },
    },
    data () {
      return {
        jiegou:{
          label: 'name', value: 'id', children: 'bizGoodsCategoryList',disabled: 'status'
        },
        fenleioptions:[],
        isThemeList: null,
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          title: {
            rules: [
              { required: true, message: '请输入标题!'},
            ]
          },
          /*goodsCategoryId:{
            rules: [
              { required: true, message: '请选择分类!'},
            ]
          },*/
          bannerUrl: {
            rules: [
              { required: true, message: '请输入地址!'},
            ]
          },
          type: {
            rules: [
              { required: true, message: '请输入类型!'},
            ]
          },
          sortNum:{
            rules: [
              { required: true, message: '请输入排序号!'},
            ]
          },
          isEnabled: {
            rules: [
              { required: true, message: '请选择是否启用!'},
            ]
          },
        },
        url: {
          add: "/content/banner/add",
          edit: "/content/banner/edit",
          queryById: "/content/banner/queryById",
          treeList:"/goods/goodsCategory/treeList"
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      onChange(w){
        console.log(w);
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'title','href','bannerUrl','type','sortNum','isTheme','isEnabled','colorValue','goodsCategoryIds'))
          let rec= JSON.stringify(record)=='{}'
          if(rec){
            this.isThemeList!=null?this.form.setFieldsValue({'isTheme':this.isThemeList[0].value}): ""
          }
        })

        getAction(this.url.treeList).then((res)=>{
          console.log(res)
          if (res.success){

            let arr = []
            // this.fenleioptions = res.result
            for (let resultElement of res.result) {
              let obj = {}
              if (resultElement.status == 1){
                obj.disabled = false
              }else {
                obj.disabled = true
              }
              //循环一级分类
              obj.value = resultElement.id
              obj.label = resultElement.name

              //循环二级分类
              if (resultElement.hasChild){
                obj.children = []
                for (let bizGoodsCategoryListElement of resultElement.bizGoodsCategoryList) {
                  let obj1 = {}
                  obj1.value = bizGoodsCategoryListElement.id
                  obj1.label = bizGoodsCategoryListElement.name
                  if (bizGoodsCategoryListElement.status == 1){
                    obj1.disabled = false
                  }else {
                    obj1.disabled = true
                  }
                  obj.children.push(obj1)
                }
              }
              arr.push(obj)
            }
            this.fenleioptions = arr
          }else {
            this.$message.error(res.message)
          }



        })

      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'title','href','bannerUrl','type','sortNum','isTheme','isEnabled','colorValue','goodsCategoryIds'))
      },
    }
  }
</script>